﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace Bonobo.Irc.Client
{
    class Debug
    {
        private static void Main()
        {
            Console.SetWindowSize(135, 50);

            //using (var conn = new IrcClientConnection(new Uri("irc://irc.felk.cvut.cz:6667")))
            using (var conn = new IrcClientConnection(new Uri("irc://irc.dal.net:6667")))
            {
                conn.MessageReceived += OnMessageReceived;
                conn.StateChanged += OnStateChanged;

                conn.SendAsync(
                    new IrcMessage(null, "PASS", "xyx"),
                    new IrcMessage(null, "USER", "bonobo", "0", "*", "bonobo"), 
                    new IrcMessage(null, "NICK", "b0n0b0"));

                while (true)
                {
                    var msg = (IrcMessage)(null);
                    var str = Console.ReadLine();

                    if (IrcMessage.TryParseAsReadOnly(str + "\r\n", out msg))
                    {
                        conn.SendAsync(msg);
                    }
                    else
                    {
                        Console.WriteLine("Syntax error!");
                    }
                }
            }
        }

        private static void OnStateChanged(Object sender, IrcConnectionStateEventArgs e)
        {
            Console.WriteLine("OnStateChanged: {0}", e.State);

            if (e.FaultReason != null)
            {
                Console.WriteLine("FaultReason = {0}", e.FaultReason);
            }
        }

        private static void OnMessageReceived(Object sender, IrcMessageEventArgs e)
        {
            Console.Write(e.Message);
        }
    }
}
